package com.renwei.yunlong.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.google.gson.Gson;
import com.kaopiz.kprogresshud.KProgressHUD;
import com.renwei.yunlong.view.NoticeMessage;

import java.util.ArrayList;
import java.util.List;

import io.rong.imlib.model.MessageContent;
import io.rong.message.ImageMessage;
import io.rong.message.InformationNotificationMessage;
import io.rong.message.RichContentMessage;
import io.rong.message.TextMessage;
import io.rong.message.VoiceMessage;


/**
 * Created by jingbin on 2016/11/22.
 * 获取原生资源
 */
public class CommonUtils {
    private KProgressHUD mProgressDialog;//进度窗体
    private static volatile CommonUtils commonUtils;//构建单例模式
    private static Gson mGson;

    public static CommonUtils getInstance() {
        if (commonUtils == null) {
            synchronized (CommonUtils.class) {
                if (commonUtils == null) {
                    commonUtils = new CommonUtils();
                }
            }
        }
        return commonUtils;
    }

    /**
     * 获取gson
     */
    public static Gson getGson() {
        if (mGson != null) {
            mGson = new Gson();
        }
        return mGson;
    }


    public static Drawable getDrawable(Context context, int resid) {
        return getResoure(context).getDrawable(resid);
    }

    public static int getColor(Context context, int resid) {
        return getResoure(context).getColor(resid);
    }

    public static Resources getResoure(Context context) {
        return context.getResources();
    }


    /**
     * 字符串是否为空
     *
     * @param input
     * @return
     */
    public static boolean isEmpty(String input) {
        if (input == null || "".equals(input) || "null".equals(input)) {
            return true;
        }

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
                return false;
            }
        }
        return true;
    }

    //  设置维度消息
    public static void setBarTextView(TextView tv, int count) {
        if (count == 0) {
            tv.setVisibility(View.GONE);
        } else {
            tv.setVisibility(View.VISIBLE);
            if (count >= 100) {
                tv.setText("99+");
            } else {
                tv.setText(count + "");
            }
        }
    }

    /**
     * 设置消息 内容
     *
     * @param tv
     * @param messageContent
     */
    public static void setRongIMContent(TextView tv, MessageContent messageContent) {
        String str = "";
        if (messageContent != null) {
            if (messageContent instanceof TextMessage) {
                str = ((TextMessage) messageContent).getContent();
            } else if (messageContent instanceof ImageMessage) {
                str = "图片";
            } else if (messageContent instanceof VoiceMessage) {
                str = "语音";
            } else if (messageContent instanceof InformationNotificationMessage) {
                str = ((InformationNotificationMessage) messageContent).getMessage();
            } else if (messageContent instanceof RichContentMessage) {
                str = "公告";
            } else if (messageContent instanceof NoticeMessage) {
                str = "公告";
            } else {
                str = "文件";
            }
        }
        tv.setText(str);
    }

    public static void showKeyboard(Context context, View view) {
        InputMethodManager im = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        im.showSoftInput(view, 0);
    }

    // 分割成两个数组
    public static List<String[]> getListIntArray(String[] strs, int size) {
        List<String[]> list = new ArrayList<String[]>();
        // tyy 取整代表可以拆分的数组个数
        String[] strs1 = new String[size];
        String[] strs2 = new String[strs.length - size];
        for (int i = 0; i < strs.length; i++) {
            if (i < size) {
                strs1[i] = strs[i];
            } else {
                strs2[i - size] = strs[i];
            }
        }
        list.add(strs1);
        list.add(strs2);
        return list;
    }

}
